﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using UrlHistoryLibrary;

namespace WebAutoUtils
{
    public class Utils
    {
        public static bool SendEmail(string smtpServer,string toEmailAddress,string fromEmail,string subject,string bodyMessage,ref string errorMessage)
        {
            bool returnValue = false;
            try
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.To.Add(toEmailAddress);
                message.Subject = subject;
                message.From = new System.Net.Mail.MailAddress(fromEmail);
                message.IsBodyHtml = true;
                message.Body = bodyMessage;
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer);
                //smtp.Send(message);
                smtp.SendCompleted += _mailDeliveryComplete;
                smtp.SendAsync(message, message);

                returnValue = true;
            }catch(Exception ex)
            {
                errorMessage = "Error while sending email:" + ex.Message;

            }
            return returnValue;
        }

        private static void _mailDeliveryComplete(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //handle error
            }
            else if (e.Cancelled)
            {
                //handle cancelled
            }
            else
            {
                //handle sent email
                // MailMessage message = (MailMessage)e.UserState;
            }
        }

        public static string SendEmail(string smtpServer, string toEmailAddress, string fromEmail, string subject, string bodyMessage)
        {
            string returnValue = "Success";
            try
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.To.Add(toEmailAddress);
                message.Subject = subject;
                message.From = new System.Net.Mail.MailAddress(fromEmail);
                message.IsBodyHtml = true;
                message.Body = bodyMessage;
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer);
                smtp.Send(message);
                
            }
            catch (Exception ex)
            {
                returnValue = "Error while sending email:" + ex.Message;

            }
            return returnValue;
        }

        public static string GetVersionNumber()
        {
            string version = "";
            try
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
                version = fvi.ProductVersion;
            }catch{}
            return version;
        }

        public static ArrayList GetIEURLHistory()
        {
            
            var urlHistory = new UrlHistoryWrapperClass();
            ArrayList list = new ArrayList();
            var enumerator=urlHistory.GetEnumerator();
            enumerator.GetUrlHistory(list);
            return list;

        }

    }
}
